<template>
  <el-dialog :visible.sync="dialog" :title="isAdd ? '新增用户' : '编辑用户'" append-to-body width="1200px" @close='closeDialog'>
    <el-form ref="form"  :model="form" :rules="rules" size="small" label-width="150px">
        	
      <!-- <el-form-item label="检查对象" prop="checkType" style="display:inline-block;width:48%;">
        <el-input v-model="form.checkType" style="width: 370px;"/>
      </el-form-item>
      
	  <el-form-item label="检查对象" prop="checkObj" style="display:inline-block;width:48%;">
        <el-radio v-for="item in dictmap.loancheck_obj" :key="item.id" v-model="form.checkObj" :label="item.value">{{ item.label }}</el-radio>
      </el-form-item> -->

	   <el-form-item label="检查对象"  style="display:inline-block;width:48%;">
           <el-select v-model="form.checkObj"  placeholder="请先选择检查对象" style="width: 370px;">
				<el-option
					v-for="item in dictmap.loancheck_obj"
					:key="item.id"
					:label="item.label"
					:value="item.value"/>
          </el-select>
      </el-form-item>
	  <el-form-item label="检查类型"  style="display:inline-block;width:48%;">
           <el-select v-model="form.checkType"  placeholder="请先选择检查类型" style="width: 370px;">
				<el-option
					v-for="item in dictmap.loancheck_type"
					:key="item.id"
					:label="item.label"
					:value="item.value"/>
          </el-select>
      </el-form-item>
	  <el-form-item label="检查方式"  style="display:inline-block;width:48%;">
           <el-select v-model="form.checkMode"  placeholder="请先选择检查方式" style="width: 370px;">
				<el-option
					v-for="item in dictmap.loancheck_mode"
					:key="item.id"
					:label="item.label"
					:value="item.value"/>
          </el-select>
      </el-form-item>
	  <el-form-item label="任务生成规则"  style="display:inline-block;width:48%;">
           <el-select v-model="form.rue"  placeholder="请先选择任务生成规则" style="width: 370px;">
				<el-option
					v-for="item in dictmap.loancheck_rule"
					:key="item.id"
					:label="item.label"
					:value="item.value"/>
          </el-select>
      </el-form-item>
      <el-form-item label="检查频率"  style="display:inline-block;width:48%;">
           <el-select v-model="form.checkFrequency"  placeholder="请先选择检查频率" style="width: 370px;">
				<el-option
					v-for="item in dictmap.loancheck_frequency"
					:key="item.id"
					:label="item.label"
					:value="item.value"/>
          </el-select>
      </el-form-item>

	  <!-- <el-form-item label="检查频率"  style="display:inline-block;width:48%;">
        <el-radio v-for="item in dictmap.loancheck_frequency" :key="item.id" v-model="form.checkFrequency" :label="item.value">{{ item.label }}</el-radio>
      </el-form-item>  -->

	   <el-form-item  style="margin-bottom: 20px;display:inline-block;width:93.1%;" label="执行机构" >
        <treeselect v-model="form.operator.id" :options="depts" style="width: 100%" placeholder="选择上级类目" />
      </el-form-item>

	  <el-form-item label="执行期限"  style="display:inline-block;width:48%;">
           <el-select v-model="form.operatorPeriod"  placeholder="请先选择任务生成规则" style="width: 370px;">
				<el-option
					v-for="item in dictmap.loancheck_term"
					:key="item.id"
					:label="item.label"
					:value="Number(item.value)"/>
          </el-select>
      </el-form-item>

	  <el-form-item label="未完成措施"  style="display:inline-block;width:48%;">
           <el-select v-model="form.measure"  placeholder="请先选择任务生成规则" style="width: 370px;">
				<el-option
					v-for="item in dictmap.loancheck_measure"
					:key="item.id"
					:label="item.label"
					:value="item.value"/>
          </el-select>
      </el-form-item>

	   <!-- <el-form-item  style="margin-bottom: 20px;display:inline-block;width:93.1%;" label="录入单位" >
        <treeselect v-model="form.createDept.id" :options="depts" style="width: 100%" placeholder=""  :disabled="true"/>
      </el-form-item> -->

	   <el-form-item label="录入单位"  style="display:inline-block;width:48%;" v-if="form.createDept!==null">
         <el-input  :disabled="true"  v-model="form.createDept.name" style="width: 370px;"/>
      </el-form-item>

	   <el-form-item label="录入日期" style="display:inline-block;width:48%;">
			<el-form-item prop="date1">
					<el-date-picker  type="date" :disabled="true"  v-model="form.createTime" style="width: 370px;" ></el-date-picker>
			</el-form-item>			
	  </el-form-item>
	  <el-form-item label="录入人"  style="display:inline-block;width:48%;" v-if="form.createUser!==null">
         <el-input  :disabled="true"  v-model="form.createUser.username" style="width: 370px;"/>
      </el-form-item>

	    <el-form-item label="更新日期" style="display:inline-block;width:48%;">
			<el-form-item prop="date1">
					<el-date-picker  type="date" :disabled="true"  v-model="form.updateTime" style="width: 370px;"></el-date-picker>
			</el-form-item>			
	  </el-form-item>

	   <el-form-item label="更新人"  style="display:inline-block;width:48%;" v-if="form.updateUser!==null">
         <el-input  :disabled="true"  v-model="form.updateUser.username" style="width: 370px;"/>
      </el-form-item>




<!-- 
      <el-form-item label="登陆账号" prop="username" style="display:inline-block;width:48%;">
        <el-input v-model="form.username" style="width: 370px;"/>
      </el-form-item>

      <el-form-item label="用户名" prop="realName" style="display:inline-block;width:48%;">
        <el-input v-model="form.realName" style="width: 370px;"/>
      </el-form-item>
	  
      <el-form-item label="所属机构"    prop="realName" style="display:inline-block;width:93.1%;">
        <treeselect v-model="deptId" :options="depts"  placeholder="选择部门" @select="selectFun" style="width: 100%;" />
      </el-form-item>

	  <el-form-item label="手机号码" prop="phone" style="display:inline-block;width:48%;margin-bottom:20px">
        <el-input v-model.number="form.phone"  style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="用户状态" prop="enabled" style="display:inline-block;width:48%;">
        <el-radio v-for="item in dicts" :key="item.id" v-model="form.enabled" :label="item.value">{{ item.label }}</el-radio>
      </el-form-item>
      <el-form-item label="联系电话" prop="telephone" style="display:inline-block;width:48%;">
        <el-input v-model="form.telephone" style="width: 370px;"/>
      </el-form-item>

      <el-form-item label="邮箱" prop="email" style="display:inline-block;width:48%;">
        <el-input v-model="form.email"  style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="岗位" style="display:inline-block;width:48%;">
        <el-select v-model="jobId"  placeholder="请先选择部门" style="width: 370px;">
          <el-option
            v-for="(item, index) in jobs"
            :key="item.name + index"
            :label="item.name"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item style="margin-bottom: 0px;display:inline-block;width:48%;" label="角色">
        <el-select v-model="roleIds" style="width: 370px;" multiple placeholder="请选择">
          <el-option
            v-for="(item, index) in roles"
            :disabled="level !== 1 && item.level <= level"
            :key="item.name + index"
            :label="item.name"
            :value="item.id"/>
        </el-select>
      </el-form-item>

      <el-form-item  label="备注"  style="display:inline-block;width:93.1%;">
            <el-input  type="textarea"  v-model="form.remark"  style="width:100%"></el-input>
      </el-form-item>
	
	  <el-form-item label="录入日期" style="display:inline-block;width:48%;">
			<el-form-item prop="date1">
					<el-date-picker  type="date" :disabled="true"  v-model="form.createTime" style="width: 370px;" ></el-date-picker>
			</el-form-item>			
	  </el-form-item>
	
	  <el-form-item label="更新日期" style="display:inline-block;width:48%;">
			<el-form-item prop="date1">
					<el-date-picker  type="date" :disabled="true"  v-model="form.updateTime" style="width: 370px;" ></el-date-picker>
			</el-form-item>			
	  </el-form-item>
	  <el-form-item label="更新人"  style="display:inline-block;width:48%;">
        <el-input v-model="form.updateUser" :disabled="true" style="width: 370px;"/>
      </el-form-item> -->
	  

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { editRule } from '@/api/loanManageInfo'

import { add, edit } from '@/api/user'
import { getDepts } from '@/api/dept'
import { getAll, getLevel } from '@/api/role'
import { getAllJob } from '@/api/job'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: { Treeselect },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    dicts: {
      type: Array,
      required: true
	},
	 dictmap: {
      type: Object,
      required: true
	},
  },
  data() {
    const validPhone = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入电话号码'))
      } else if (!this.isvalidPhone(value)) {
        callback(new Error('请输入正确的11位手机号码'))
      } else {
        callback()
      }
    }
    return {
	  dialog: false, loading: false, 
	//  form: { username: '',realName:null, email: '', enabled: 'false', roles: [], job: { id: '' }, dept: { id: '' }, phone: null,telephone:null,remark:null,createDept:null,createTime:null,createUser:null,updateTime:null,updateUser:null},
	  form:{
		  id:'',
		  checkType:null,
		  checkObj:'',
		  checkMode:null,
		  rue:null,
		  checkFrequency:'',
		  createDept:null,
		 // operator:null,
		 operator:{id:''},

		//  createDept:{id:''},
		  operatorPeriod:null,
		  measure:null,
		  createTime:null,
		  createUser:null,
		  updateTime:null,
		  updateUser:null

	  },
	  roleIds: [], roles: [], depts: [], deptId: null, jobId: null, jobs: [], style: 'width: 184px', level: 3,
      rules: {
        username: [
          { required: true, message: '请输入登陆账号', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
		],
		realName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
		],
		deptId: [
          { required: true, message: '请选择上级机构', trigger: 'change' }
		],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ],
        phone: [
          { required: true, trigger: 'blur', validator: validPhone }
        ],
        enabled: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
		],
		telephone: [
          { required: true, message: '联系方式不能为空', trigger: 'blur' }
		],
		
      }
    }
  },
  created() {
	

    const explorer = navigator.userAgent
    if (explorer.indexOf('Chrome') >= 0) {
      this.style = 'width: 184px'
    } else {
      this.style = 'width: 172px'
    }
  },
  mounted: function() {
       this.$nextTick(() => {
               setTimeout(() => {
		           this.form = sessionStorage.getItem('ruleDetails')
                   console.log('this.form+++----',this.form)
	         },500) 
	   })
  },
  methods: {
	closeDialog(){
		this.resetForm()
	},
    cancel() {
      this.resetForm()
	},
	doSubmit() {
		console.log('isAdd',this.isAdd)
		if(!this.isAdd){
		        const params = {
						id:this.form.id,
					//	dept:{id:this.form.createDept.id},
					    operator:{id:this.form.operator.id},					
						checkObj:this.form.checkObj,
						checkType:this.form.checkType,
						checkMode:this.form.checkMode,
						rue:this.form.rue,
						checkFrequency:this.form.checkFrequency,
						operatorPeriod:this.form.operatorPeriod.toString(),
						measure:this.form.measure,
	             }
			     editRule(params).then(res => {
					this.resetForm()
					this.$notify({
					title: '修改成功',
					type: 'success',
					duration: 2500
					})
					this.loading = false
					this.$parent.init()
				}).catch(err => {
					this.loading = false
					console.log(err.response.data.message)
				})
		}
     // this.form.dept.id = this.deptId
     // this.form.job.id = this.jobId
    //   this.$refs['form'].validate((valid) => {
    //     if (valid) {
    //       if (this.deptId === null || this.deptId === undefined) {
    //         this.$message({
    //           message: '部门不能为空',
    //           type: 'warning'
    //         })
    //       } else if (this.jobId === null) {
    //         this.$message({
    //           message: '岗位不能为空',
    //           type: 'warning'
    //         })
    //       } else if (this.roleIds.length === 0) {
    //         this.$message({
    //           message: '角色不能为空',
    //           type: 'warning'
    //         })
    //       } else {
    //         this.loading = true
    //         this.form.roles = []
    //         const _this = this
    //         this.roleIds.forEach(function(data, index) {
    //           const role = { id: data }
    //           _this.form.roles.push(role)
	// 		})
		
    //         if (this.isAdd) {
    //           this.doAdd()
    //         } else this.doEdit()
    //       }
    //     } else {
    //       return false
    //     }
    //   })
    },
    // doSubmit() {
    //   this.form.dept.id = this.deptId
    //   this.form.job.id = this.jobId
    //   this.$refs['form'].validate((valid) => {
    //     if (valid) {
    //       if (this.deptId === null || this.deptId === undefined) {
    //         this.$message({
    //           message: '部门不能为空',
    //           type: 'warning'
    //         })
    //       } else if (this.jobId === null) {
    //         this.$message({
    //           message: '岗位不能为空',
    //           type: 'warning'
    //         })
    //       } else if (this.roleIds.length === 0) {
    //         this.$message({
    //           message: '角色不能为空',
    //           type: 'warning'
    //         })
    //       } else {
    //         this.loading = true
    //         this.form.roles = []
    //         const _this = this
    //         this.roleIds.forEach(function(data, index) {
    //           const role = { id: data }
    //           _this.form.roles.push(role)
    //         })
    //         if (this.isAdd) {
    //           this.doAdd()
    //         } else this.doEdit()
    //       }
    //     } else {
    //       return false
    //     }
    //   })
    // },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          message: '默认密码：123456',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
	//  const bridge = {
	// 	 id:JSON.parse(sessionStorage.getItem('userAvatar')).id,
	// 	 path:JSON.parse(sessionStorage.getItem('userAvatar')).path,
	// 	 size:JSON.parse(sessionStorage.getItem('userAvatar')).size,
	// 	 realName:this.form.realName
	//  }
    //  const params = {
	// 	 createDept:this.form.createDept,
	// 	 createTime:this.form.createTime,
	// 	 createUser:this.form.createUser,
	// 	 dept:this.form.dept,
	// 	 email:this.form.email,
	// 	 enabled:this.form.enabled,
	// 	 id:this.form.id,
	// 	 job:this.form.job,
	// 	 phone:this.form.phone,
	// 	 remark:this.form.remark,
	// 	 roles:this.form.roles,
	// 	 telephone:this.form.telephone,
    //      username:this.form.username,
	// 	// userAvatar:bridge
	// 	 realName:this.form.realName
	//  }
	  
	 const params = {
		 dept:this.form.dept,
		 email:this.form.email,
		 enabled:this.form.enabled,
		 id:this.form.id,
		 job:this.form.job,
		 phone:this.form.phone,
		 remark:this.form.remark,
		 roles:this.form.roles,
		 telephone:this.form.telephone,
         username:this.form.username,
		// userAvatar:bridge
		 realName:this.form.realName
	 }

      edit(params).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
   //   this.deptId = null
    //  this.jobId = null
    //  this.roleIds = []
     // this.form = { username: '', email: '', enabled: 'false', roles: [], job: { id: '' }, dept: { id: '' }, phone: null }
    },
    getRoles() {
      getAll().then(res => {
        this.roles = res
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    getJobs(id) {
      getAllJob(id).then(res => {
        this.jobs = res.content
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    getDepts() {
      getDepts({ enabled: true }).then(res => {
        this.depts = res.content
      })
    },
    isvalidPhone(str) {
      const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
      return reg.test(str)
    },
    selectFun(node, instanceId) {
	  this.getJobs(node.id)
    },
    getRoleLevel() {
      getLevel().then(res => {
        this.level = res.level
      }).catch(err => {
        console.log(err.response.data.message)
      })
    }
  }
}
</script>

<style scoped>
.el-form-item__content{
	width:86.12%;
}

</style>
